package yt.demo.demoadmin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import yt.demo.appcommonbean.http.DeleteRequest;
import yt.demo.appcommonbean.http.DetailRequest;
import yt.demo.appcommonbean.http.RestResponse;
import yt.demo.appcommondb.base.BaseEntity;
import yt.demo.appcommondb.dto.SplitPageDTO;
import yt.demo.appcommondb.vo.PageVO;
import yt.demo.appcorequartz.entity.QuartzJob;
import yt.demo.appcorequartz.entity.QuartzLog;
import yt.demo.appcorequartz.service.ICronService;
import yt.demo.appcorequartz.service.IQuartzLogService;

@RestController
@RequestMapping("/cron")
public class CronController {

    private final ICronService service;
    private final IQuartzLogService quartzLogService;

    @Autowired
    public CronController(ICronService service, IQuartzLogService quartzLogService) {
        this.service = service;
        this.quartzLogService = quartzLogService;
    }

    @GetMapping("/getPageList")
    public RestResponse<PageVO<QuartzJob>> getPageList(SplitPageDTO page, QuartzJob params) {
        return RestResponse.ok(this.service.getPageList(page, params));
    }

    @PostMapping("/create")
    public RestResponse<Void> create(@RequestBody  @Validated(BaseEntity.CreateGroup.class) QuartzJob req) {
        this.service.create(req);
        return RestResponse.ok();
    }

    @PostMapping("/update")
    public RestResponse<Void> update(@RequestBody @Validated(BaseEntity.UpdateGroup.class) QuartzJob req) {
        this.service.update(req);
        return RestResponse.ok();
    }

    @PostMapping("/delete")
    public RestResponse<Void> delete(@RequestBody DeleteRequest req) {
        this.service.delete(req);
        return RestResponse.ok();
    }

    @PostMapping("/updateStatus")
    public RestResponse<Void> updateStatus(@RequestBody QuartzJob req) {
        this.service.updateStatus(req);
        return RestResponse.ok();
    }

    @PostMapping("/run")
    public RestResponse<Void> run(@RequestBody DetailRequest req) {
        this.service.run(req);
        return RestResponse.ok();
    }

    @GetMapping("/getLogPageList")
    public RestResponse<PageVO<QuartzLog>> getLogPageList(SplitPageDTO page, QuartzLog params) {
        return RestResponse.ok(this.quartzLogService.getPageList(page, params, w->w.orderByDesc(QuartzLog::getId), null));
    }

    @PostMapping("/logDelete")
    public RestResponse<Void> logDelete(@RequestBody DeleteRequest req) {
        this.quartzLogService.delete(req);
        return RestResponse.ok();
    }

}
